//
//  CalculatorViewController.m
//  Calculator
//
//  Created by lding4 on 10/16/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorViewController.h"


@implementation CalculatorViewController

@synthesize upperLabel, lowerLabel;

- (void)InitCalculator
{
    // make sure the upper label show nothing
    upperLabel.text=@"";
    // lower label show 0
    lowerLabel.text=@"0";
    // init all state
    [self InitState];
}
- (void)InitState
{
    state=State_Init;
    NegativeSign = FALSE;
}

- (void)InitInput
{
    if (state & State_Input){
        [self RemoveState:State_Input];
    }
    if (state & State_Input_Dot) {
        [self RemoveState:State_Input_Dot];
    }
    if (state & State_Input_Ful) {
        [self RemoveState:State_Input_Ful];
    }
    NegativeSign = FALSE;
    [self AddState:State_Init];
    lowerLabel.text = @"0";
}

- (void)AddState:(unsigned)NewState
{
    state |= NewState;
}
- (void)RemoveState:(unsigned)OldState
{
    state ^= OldState;
}

- (void)ShowLabelData:(double)NumberContent with:(UILabel *)ContentShow
{
    long tempData = (long)NumberContent;
    if(0.000001>(NumberContent-tempData))
    {
        ContentShow.text = [NSString stringWithFormat:@"%d", tempData];
    }
    else if(0.00001>(NumberContent*10-(long)(NumberContent*10)))
    {
        ContentShow.text = [NSString stringWithFormat:@"%.1f", NumberContent];
    }
    else if(0.0001>(NumberContent*100-(long)(NumberContent*100)))
    {
        ContentShow.text = [NSString stringWithFormat:@"%.2f", NumberContent];
    }
    else if(0.001>(NumberContent*1000-(long)(NumberContent*1000)))
    {
        ContentShow.text = [NSString stringWithFormat:@"%.3f", NumberContent];
    }
    else if(0.01>(NumberContent*10000-(long)(NumberContent*10000)))
    {
        ContentShow.text = [NSString stringWithFormat:@"%.4f", NumberContent];
    }
    else if(0.1>(NumberContent*100000-(long)(NumberContent*100000)))
    {
        ContentShow.text = [NSString stringWithFormat:@"%.5f", NumberContent];
    }
    else
    {
        ContentShow.text = [NSString stringWithFormat:@"%f", NumberContent];
    }
}

- (IBAction)ButtonOnePressed:(UIButton *)button{
    
    NSString *title = [button titleForState:UIControlStateNormal];
    int number = [title intValue];
    
    if (state & State_Init) {
        if(NegativeSign){
            [lowerLabel setText:[NSString stringWithFormat:@"-%d", number]];
//            [lowerLabel setText:@"-1"];
        }
        else
        {
            [lowerLabel setText:[NSString stringWithFormat:@"%d", number]];
//            [lowerLabel setText:@"1"];           
        }
        
        [self AddState:State_Input];
        [self RemoveState:State_Init];
    }
    else if (state & State_Input) {
        [lowerLabel setText:[lowerLabel.text stringByAppendingString:[NSString stringWithFormat:@"%d", number]]];
//        [lowerLabel setText:[lowerLabel.text stringByAppendingString:@"1"]];
    }
    else if (state & State_Output) {
        lowerLabel.text = [NSString stringWithFormat:@"%d", number];
        [self RemoveState:State_Output];
        [self AddState:State_Input];
    }
}

- (IBAction)ButtonZeroPressed:(UIButton *)button{
    
    NSString *title = [button titleForState:UIControlStateNormal];
    int number = [title intValue];
    
    if (state & State_Init) {
        ;
    }
    else if (state & State_Input) {
        [lowerLabel setText:[lowerLabel.text stringByAppendingString:@"0"]];
        //        [lowerLabel setText:[lowerLabel.text stringByAppendingString:@"1"]];
    }
    else if (state & State_Output) {
        lowerLabel.text = [NSString stringWithFormat:@"%d", number];
        [self RemoveState:State_Output];
        [self AddState:State_Init];
    }
}

- (IBAction)ButtonPlusPressed{
    if(state & State_Error) {
        return;//Do nothing
    }
    else if((state&State_Upper_Data) && (state&State_Init))// have no input, just change the operation symbol
    {
        ;
    }
    else if(state & State_Upper_Data)
    {
        //string_number conversion and calculation 
        double temp = [lowerLabel.text doubleValue];
        NSLog(@"conversion result: %f\n", temp);
        // calculate the result
        switch (operation) {
            case 0x01:
                upperData = upperData+temp;
                break;
            case 0x02:
                upperData = upperData-temp;
                break;
            case 0x03:
                upperData = upperData*temp;
                break;
            case 0x04:
                if(0 == temp)
                {
                    [self AddState:State_Error];
                    upperLabel.text = @"Error!";
                    lowerLabel.text = @"Error!";
                    return;
                }
                else{
                    upperData = upperData/temp;
                }
            default:
                break;
        }
    }
    else if(state & State_Input) 
    {
        //Stransfer the data to label 2
        upperData = [lowerLabel.text doubleValue];
        [self AddState:State_Upper_Data];
    }
    else if(state & State_Output)
    {
        [self RemoveState:State_Output];
        [self AddState:State_Upper_Data];
    }
    else
    {
        NSLog(@"unknown pace, how can you get here?");
    }
    //update the calculation data to label 2
    [self ShowLabelData:upperData with:upperLabel];
    
    //attach the operation symbol to label 2
    [upperLabel setText:[upperLabel.text stringByAppendingString:@" +"]];
    [self InitInput];
    operation = 0x01;
}

- (IBAction)ButtonDevidePressed{
    if(state & State_Error) {
        return;//Do nothing
    }
    else if((state&State_Upper_Data) && (state&State_Init))// have no input, just change the operation symbol
    {
        ;
    }
    else if(state & State_Upper_Data)
    {
        //string_number conversion and calculation 
        double temp = [lowerLabel.text doubleValue];
        NSLog(@"conversion result: %f\n", temp);
        // calculate the result
        switch (operation) {
            case 0x01:
                upperData = upperData+temp;
                break;
            case 0x02:
                upperData = upperData-temp;
                break;
            case 0x03:
                upperData = upperData*temp;
                break;
            case 0x04:
                if(0 == temp)
                {
                    [self AddState:State_Error];
                    upperLabel.text = @"Error!";
                    lowerLabel.text = @"Error!";
                    return;
                }
                else{
                    upperData = upperData/temp;
                }
            default:
                break;
        }
    }
    else if(state & State_Input) 
    {
        //Stransfer the data to label 2
        upperData = [lowerLabel.text doubleValue];
        [self AddState:State_Upper_Data];
    }
    else if(state & State_Output)
    {
        [self RemoveState:State_Output];
        [self AddState:State_Upper_Data];
    }
    else
    {
        NSLog(@"unknown pace, how can you get here?");
    }
    //update the calculation data to label 2
    [self ShowLabelData:upperData with:upperLabel];
    //attach the operation symbol to label 2
    [upperLabel setText:[upperLabel.text stringByAppendingString:@" ÷"]];
    [self InitInput];
    operation = 0x04;
}

- (IBAction)ButtonMultiplyPressed{
    if(state & State_Error) {
        return;//Do nothing
    }
    else if((state&State_Upper_Data) && (state&State_Init))// have no input, just change the operation symbol
    {
        ;
    }
    else if(state & State_Upper_Data)
    {
        //string_number conversion and calculation 
        double temp = [lowerLabel.text doubleValue];
        NSLog(@"conversion result: %f\n", temp);
        // calculate the result
        switch (operation) {
            case 0x01:
                upperData = upperData+temp;
                break;
            case 0x02:
                upperData = upperData-temp;
                break;
            case 0x03:
                upperData = upperData*temp;
                break;
            case 0x04:
                if(0 == temp)
                {
                    [self AddState:State_Error];
                    upperLabel.text = @"Error!";
                    lowerLabel.text = @"Error!";
                    return;
                }
                else{
                    upperData = upperData/temp;
                }
            default:
                break;
        }
    }
    else if(state & State_Input) 
    {
        //Stransfer the data to label 2
        upperData = [lowerLabel.text doubleValue];
        [self AddState:State_Upper_Data];
    }
    else if(state & State_Output)
    {
        [self RemoveState:State_Output];
        [self AddState:State_Upper_Data];
    }
    else
    {
        NSLog(@"unknown pace, how can you get here?");
    }
    //update the calculation data to label 2
    [self ShowLabelData:upperData with:upperLabel];
    //attach the operation symbol to label 2
    [upperLabel setText:[upperLabel.text stringByAppendingString:@" ×"]];
    [self InitInput];
    operation = 0x03;
}

- (IBAction)ButtonMinusPressed{
    if(state & State_Error) {
        ;//Do nothing
    }
    else if((state&State_Upper_Data) && (state&State_Init))// have no input, just change the operation symbol
    {
        ;
    }
    else if(state & State_Upper_Data)
    {
        //string_number conversion and calculation 
        double temp = [lowerLabel.text doubleValue];
        NSLog(@"conversion result: %f\n", temp);
        // calculate the result
        switch (operation) {
            case 0x01:
                upperData = upperData+temp;
                break;
            case 0x02:
                upperData = upperData-temp;
                break;
            case 0x03:
                upperData = upperData*temp;
                break;
            case 0x04:
                if(0 == temp)
                {
                    [self AddState:State_Error];
                    upperLabel.text = @"Error!";
                    lowerLabel.text = @"Error!";
                    return;
                }
                else{
                    upperData = upperData/temp;
                }
            default:
                break;
        }
    }
    else if(state & State_Input) 
    {
        //Stransfer the data to label 2
        upperData = [lowerLabel.text doubleValue];
        [self AddState:State_Upper_Data];
    }
    else if(state & State_Output)
    {
        [self RemoveState:State_Output];
        [self AddState:State_Upper_Data];
    }
    else
    {
        NSLog(@"unknown pace, how can you get here?");
    }
    //update the calculation data to label 2
    [self ShowLabelData:upperData with:upperLabel];
    //attach the operation symbol to label 2
    [upperLabel setText:[upperLabel.text stringByAppendingString:@" -"]];
    [self InitInput];
    operation = 0x02;
}

- (IBAction)ButtonEqualPressed{
    if (state & State_Error) {
        ;// do nothing
    }
    else if(state & State_Upper_Data)
    {
        //string_number conversion and calculation 
        double temp = [lowerLabel.text doubleValue];
        NSLog(@"conversion result: %f\n", temp);
        // calculate the result
        switch (operation) {
            case 0x01:
                upperData = upperData+temp;
                break;
            case 0x02:
                upperData = upperData-temp;
                break;
            case 0x03:
                upperData = upperData*temp;
                break;
            case 0x04:
                if(0 == temp)
                {
                    [self AddState:State_Error];
                    upperLabel.text = @"Error!";
                    lowerLabel.text = @"Error!";
                    return;
                }
                else{
                    upperData = upperData/temp;
                }
            default:
                break;
        }
        // clear data in upper label
        upperLabel.text = @"";
        // show the result in lower label
        [self ShowLabelData:upperData with:lowerLabel];
        
        // clean all input status
        if (state & State_Input){
            [self RemoveState:State_Input];
        }
        if (state & State_Input_Dot) {
            [self RemoveState:State_Input_Dot];
        }
        if (state & State_Input_Ful) {
            [self RemoveState:State_Input_Ful];
        }
        if (state & State_Upper_Data) {
            [self RemoveState:State_Upper_Data];
        }
        NegativeSign = FALSE;
        [self AddState:State_Output];
    }
}

- (IBAction)ButtonNegPressed{
    if ((state & State_Init) || (state&State_Input)) {
        if(NegativeSign)// already negative sign, remove it
        {
            double temp = [lowerLabel.text doubleValue];
            temp=(-1)*temp;
            [self ShowLabelData:temp with:lowerLabel];
            NegativeSign = FALSE;

        }
        else// no negative sign, add it
        {
            [lowerLabel setText:[@"-" stringByAppendingString:lowerLabel.text]];
            NegativeSign = TRUE;
        }
    }
    else if (state & State_Output) {
        lowerLabel.text = @"-0";
        NegativeSign = TRUE;
        [self RemoveState:State_Output];
        [self AddState:State_Init];
    }
    else if (state & State_Error){
        return;// do nothing
    }
}

- (IBAction)ButtonDotPressed{
    if (state & State_Input_Dot || state & State_Error) {
        ;// do nothing
    }
    else if (state & State_Init) {
        //[lowerLabel setText:[lowerLabel.text stringByAppendingString:@"."]];
        lowerLabel.text = @"0.";
        [self RemoveState:State_Init];
        [self AddState:State_Input];
        [self AddState:State_Input_Dot];
    }
    else if(state & State_Input){
        [lowerLabel setText:[lowerLabel.text stringByAppendingString:@"."]];
        [self AddState:State_Input_Dot];
    }
    else if(state & State_Output){
        lowerLabel.text = @"0.";
        [self RemoveState:State_Output];
        [self AddState:State_Input];
        [self AddState:State_Input_Dot];
    }
}

- (IBAction)ButtonCPressed{
    [self InitCalculator];
}

- (IBAction)ButtonCEPressed{
    if (state & State_Error) {
        [self InitCalculator];
    }
    else{
        [self InitInput];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self InitCalculator];
}

/*
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



@end
